package com.t.testsql;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

public class Testprovider  extends ContentProvider {

    private static final UriMatcher sUriMatcher;
    private static final int MATCH_FIRST = 1;
    public static final String AUTHORITY = "com.t.testsql.Testprovider";
    public static final Uri CONTENT_URI_FIRST = Uri.parse("content://" + AUTHORITY + "/my_table");

    static {
        // 句话的意思是，如果匹配不成功，返回UriMatcher.NO_MATCH
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        // 这个path的路径是可以自己重新定义的，一般来说是已知数据库的表名，但是可以自己改成别的
        // 主要是后面那个code能够对得上就可以。
        sUriMatcher.addURI(AUTHORITY, "first", MATCH_FIRST);
      //  sUriMatcher.addURI(AUTHORITY, "my_table", MATCH_FIRST);
    }

    private Sqldatabase mdatabase;

    @Override
    public boolean onCreate() {
        mdatabase = new Sqldatabase(getContext());
        return false;
    }




    @Override
    public String getType( Uri uri) {
        return null;
    }

    @Override
    public Cursor query( Uri uri,  String[] projection,  String selection,
                         String[] selectionArgs,  String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        switch (sUriMatcher.match(uri)) {
            case MATCH_FIRST:
                // 设置查询的表
                queryBuilder.setTables(Sqldatabase.TABLE_NAME);
                break;
            default:
                throw new IllegalArgumentException("Unknow URI: " + uri);
        }

        SQLiteDatabase db = mdatabase.getReadableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, null);
        return cursor;
    }


    @Override
    public Uri insert( Uri uri,  ContentValues values) {
        SQLiteDatabase database = mdatabase.getWritableDatabase();
        Log.e("urlmatch",sUriMatcher.match(uri)+"");
        // 以下是固定代码
        switch (sUriMatcher.match(uri)){
            case MATCH_FIRST:
                long rowID = database.insert(Sqldatabase.TABLE_NAME, null, values);
                //  这个相当于重新整合了新的uri地址。加上了后面列的id，也可以不加进去
//                if(rowID > 0) {
//                    Uri retUri = ContentUris.withAppendedId(CONTENT_URI_FIRST, rowID);
//                    return retUri;
//                }
                Log.e("urlmatch","111");
                break;
        }
//        this.getContext().getContentResolver().notifyChange(uri, null);
        return null;
    }

    @Override
    public int delete( Uri uri, String selection,  String[] selectionArgs) {

        SQLiteDatabase db = mdatabase.getWritableDatabase();
        int count = 0;
        switch(sUriMatcher.match(uri)) {
            case MATCH_FIRST:
                count = db.delete(Sqldatabase.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknow URI :" + uri);
        }
        return count;

    }




    @Override
    public int update( Uri uri,  ContentValues values,  String selection,  String[] selectionArgs) {
        SQLiteDatabase db = mdatabase.getWritableDatabase();
        int count = 0;
        switch(sUriMatcher.match(uri)) {
            case MATCH_FIRST:
                count = db.update(Sqldatabase.TABLE_NAME, values, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknow URI : " + uri);
        }
        this.getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }
}
